package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.RoleDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.RoleCommand;
import com.cskaoyan.wordmemorize.request.RolePermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sgl
 * @version 1.0
 * @description: TODO
 * @Date 2025/07/16
 */
@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Autowired
    RolePermissionService rolePermissionService;

    @PostMapping("")
    @SaAdminCheckPermission("admin:role:add")
    public Result addRole(@RequestBody RoleCommand roleCommand){
        rolePermissionService.addRole(roleCommand);
        return Result.ok();
    }

    @PutMapping("")
    @SaAdminCheckPermission("admin:role:update")
    public Result updateRole(@RequestBody RoleCommand roleCommand){
        rolePermissionService.updateRole(roleCommand);
        return Result.ok();

    }

    /**
     * 账号角色授权
     * @param rolePermissionCommand
     * @return
     */
    @PutMapping("/permission")
    @SaAdminCheckPermission("admin:role:authorize")
    public Result updateRolePermission(@RequestBody RolePermissionCommand rolePermissionCommand){
        rolePermissionService.updateRolePermission(rolePermissionCommand);
        return Result.ok();
    }

    /**
     * 获取账号角色列表
     * @return
     */
    @GetMapping("/list")
    @SaAdminCheckPermission("admin:role:search")
    public Result<PageDTO> getAdminRoleList( PageRequest request){
        PageDTO<RoleDTO> roleList = rolePermissionService.getRoleList(request);
        return Result.ok(roleList);
    }

    /**
     * 删除账号角色
     * @param roleId
     * @return
     */
    @DeleteMapping("{roleId}")
    @SaAdminCheckPermission("admin:role:delete")
    public Result deleteRole(@PathVariable("roleId") String roleId){
        DeleteRoleOperateDTO deleteRoleOperateDTO = rolePermissionService.deleteRole(Long.valueOf(roleId));
        return Result.ok(deleteRoleOperateDTO);
    }



}
